#!/usr/local/python3/bin/python3
# -*- coding: UTF-8 -*-
import json
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import datetime
import re

es_index = 'tmanalyzer'
es_type = 'tradeMark'
# json文件路径
# C:/Users/Administrator/Desktop/tm_analyzer_json_path.txt
# /opt/tm_script/tm_analyzer_json_path.txt
json_path = "/opt/tm_script/tm_analyzer_json_path.txt"
province_pattern = r'([\u4e00-\u9fa5]{2,5}?(?:省|自治区|市){1})'
city_pattern = r'([\u4e00-\u9fa5]{2,5}?(?:市){1})'

# es连接获取
es = Elasticsearch(hosts=[{'host': "192.168.1.167", 'port': "9200"}], timeout=30)


central_city = ['北京市', '天津市', '上海市', '重庆市', '香港特别行政区', '澳门特别行政区']
province = ['河北省', '山西省', '内蒙古自治区', '辽宁省', '吉林省', '黑龙江省', '江苏省',
            '浙江省', '安徽省', '福建省', '江西省', '山东省', '河南省', '湖北省', '湖南省', '广东省', '广西壮族自治区',
            '海南省', '四川省', '贵州省', '云南省', '西藏自治区', '陕西省', '甘肃省', '青海省', '宁夏回族自治区'
            '新疆维吾尔自治区', '台湾省']


def is_none(data):
    if data is None:
        return True
    return False


# 日期格式化函数
def convert_date(date):
    if is_none(date):
        return None
    date_str = date.replace('T', ' ')[0:19]
    return datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')


# 日期格式化函数,带毫秒
def convert_date_with_ms(date):
    if is_none(date):
        return None
    date_str = date.replace('T', ' ')[0:23]
    return datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S.%f')


# 过滤商标名称
def filter_tm_name(tm_name):
    tm_name = tm_name.strip()
    reg_cn = re.compile("[^\u4e00-\u9fa5]")
    reg_en = re.compile("[\u4e00-\u9fa5]")
    tm_name_cn = reg_cn.sub('', tm_name)
    tm_name_en = reg_en.sub('', tm_name)
    tm_name_no_space = tm_name.replace(' ', '')
    tm_name_cn_en = tm_name_cn.replace(' ', '') + tm_name_en.replace(' ', '')
    if tm_name_no_space == tm_name_cn_en:
        tm_name_result = tm_name_cn + ' ' + tm_name_en
    else:
        tm_name_result = tm_name
    return tm_name_result.strip()


# 商标名称格式化
def convert_tm_name(tm_name):
    if is_none(tm_name):
        return None
    tm_name = filter_tm_name(tm_name)
    tm_name_array = tm_name.strip().split(' ')
    if len(tm_name_array) > 1:
        tm_name_result = ''
        for name in tm_name_array:
            if len(name.strip()) > 0:
                tm_name_result = tm_name_result + ' ' + name.strip()
        return tm_name_result.strip()
    else:
        return tm_name


# 地址格式化
def convert_address(full_address):
    if is_none(full_address):
        return None
    tmRegistrantCnAddress = {
        'fullAddress': full_address
    }
    reg_province = re.search(province_pattern, full_address)
    if reg_province:
        pro = reg_province.group()
        for central in central_city:
            if pro == central:
                tmRegistrantCnAddress['province'] = pro
                tmRegistrantCnAddress['city'] = pro
                return tmRegistrantCnAddress
        tmRegistrantCnAddress['province'] = pro
        full_address = full_address.replace(pro, '')
    else:
        return tmRegistrantCnAddress

    reg_city = re.search(city_pattern, full_address)
    if reg_city:
        tmRegistrantCnAddress['city'] = reg_city.group()
    return tmRegistrantCnAddress


# 构造元数据
def build_trademark_source(data):
    source = {}
    tm_app_no = data.get('tmAppNo', None)
    if is_none(tm_app_no) is False:
        source['tmAppNo'] = tm_app_no
    tm_category_no = data.get('tmCategoryNo', None)
    if is_none(tm_category_no) is False:
        source['tmCategoryNo'] = tm_category_no
    tm_app_date = data.get('tmAppDate', None)
    if is_none(tm_app_date) is False:
        source['tmAppDate'] = convert_date(tm_app_date['$date'])
    tm_name = data.get('tmName', None)
    if is_none(tm_name) is False:
        source['tmName'] = convert_tm_name(tm_name)
    tm_type = data.get('tmType', None)
    if is_none(tm_type) is False:
        source['tmType'] = tm_type
    tmFirstTrialNoticeIssue = data.get('tmFirstTrialNoticeIssue', None)
    if is_none(tmFirstTrialNoticeIssue) is False:
        source['tmFirstTrialNoticeIssue'] = tmFirstTrialNoticeIssue
    tmFirstTrialNoticeDate = data.get('tmFirstTrialNoticeDate', None)
    if is_none(tmFirstTrialNoticeDate) is False:
        source['tmFirstTrialNoticeDate'] = convert_date(tmFirstTrialNoticeDate['$date'])
    tmRegisterNoticeIssue = data.get('tmRegisterNoticeIssue', None)
    if is_none(tmRegisterNoticeIssue) is False:
        source['tmRegisterNoticeIssue'] = tmRegisterNoticeIssue
    tmRegisterNoticeDate = data.get('tmRegisterNoticeDate', None)
    if is_none(tmRegisterNoticeDate) is False:
        source['tmRegisterNoticeDate'] = convert_date(tmRegisterNoticeDate['$date'])
    tmSpecialIssueBeginDate = data.get('tmSpecialIssueBeginDate', None)
    if is_none(tmSpecialIssueBeginDate) is False:
        source['tmSpecialIssueBeginDate'] = convert_date(tmSpecialIssueBeginDate['$date'])
    tmSpecialIssueEndDate = data.get('tmSpecialIssueEndDate', None)
    if is_none(tmSpecialIssueEndDate) is False:
        source['tmSpecialIssueEndDate'] = convert_date(tmSpecialIssueEndDate['$date'])
    tmSpecialValidity = data.get('tmSpecialValidity', None)
    if is_none(tmSpecialValidity) is False:
        source['tmSpecialValidity'] = tmSpecialValidity
    tmMutualApplySign = data.get('tmMutualApplySign', None)
    if is_none(tmMutualApplySign) is False:
        source['tmMutualApplySign'] = tmMutualApplySign
    tmForm = data.get('tmForm', None)
    if is_none(tmForm) is False:
        source['tmForm'] = tmForm
    tmWellKnownSign = data.get('tmWellKnownSign', None)
    if is_none(tmWellKnownSign) is False:
        source['tmWellKnownSign'] = tmWellKnownSign
    source['createTime'] = convert_date_with_ms(data['createTime']['$date'])
    source['updateTime'] = convert_date_with_ms(data['updateTime']['$date'])
    tmRegistrant = data.get('tmRegistrant', None)
    if is_none(tmRegistrant) is False:
        tmRegistrantCnName = tmRegistrant.get('tmRegistrantCnName', None)
        if is_none(tmRegistrantCnName) is False:
            source['tmRegistrantCnName'] = tmRegistrantCnName
        tmRegistrantCnAddress = tmRegistrant.get('tmRegistrantCnAddress', None)
        if is_none(tmRegistrantCnAddress) is False:
            source['tmRegistrantCnAddress'] = convert_address(tmRegistrantCnAddress)
    tmProxyPerson = data.get('tmProxyPerson', None)
    if is_none(tmProxyPerson) is False:
        tmProxyPersonName = tmProxyPerson.get('tmProxyPersonName', None)
        if is_none(tmProxyPersonName) is False:
            tmProxyPerson = {'tmProxyPersonName': tmProxyPersonName}
            source['tmProxyPerson'] = tmProxyPerson
    return source


# 批量保存到es
def bulk_insert(array, s, e):
    try:
        helpers.bulk(es, array)
        print("Save success:%d to %d" % (s, e))
        return True
    except Exception:
        try:
            helpers.bulk(es, array)
            print("Save success:%d to %d" % (s, e))
            return True
        except Exception:
            try:
                helpers.bulk(es, array)
                print("Save success:%d to %d" % (s, e))
                return True
            except Exception:
                print("Save failed:%d to %d" % (s, e))
                return False


def parse_json_file():
    bulk_insert_num = 0
    # 读取json文件路径
    json_path_reader = open(json_path, encoding="utf-8")
    file_path = json_path_reader.readline().strip()
    json_path_reader.close()
    if file_path == '':
        print('Please fill in the json file path')
    else:
        print('Start reading json file : ', file_path)
        file = open(file_path.strip(), encoding="utf-8")
        line = file.readline()
        target_array = []
        while line:
            data = json.loads(line)
            # 获取json文件的数据
            source = build_trademark_source(data)
            # es插入数据格式构建
            target = {"_index": es_index, "_type": es_type, "_id": data['_id']['$oid'], "_source": source}
            target_array.append(target)
            # 数组内部数量达到1000则往es插入一次
            if len(target_array) == 1000:
                start = bulk_insert_num
                bulk_insert_num = bulk_insert_num + 1000
                end = bulk_insert_num
                bulk_insert(target_array, start, end)
                target_array.clear()

            line = file.readline()
        # 最后一次不足1000的内容插入es
        if len(target_array) > 0:
            start = bulk_insert_num
            bulk_insert_num = bulk_insert_num + len(target_array)
            end = bulk_insert_num
            bulk_insert(target_array, start, end)
            target_array.clear()
        file.close()
        print('End reading')


if __name__ == "__main__":
    parse_json_file()
